use std::collections::HashMap;
use ferris_says::say;
use std::io::{stdout,BufWriter};
use std::ops::Index;

fn main() {
    //demo1();
    //demo2();
    //demo3();
    //demo4();
    //demo5();
    //demo6();
    //demo7();
    //demo8();
    //demo9();
    //demo10();
    //demo11();
    //demo12();
    //demo13();
    demo14();
}
fn demo1(){//测试
    let stdout  = stdout();
    let message = String::from("HEllo fellow Rustaceans!");
    let width = message.chars().count();

    let mut writer = BufWriter::new(stdout.lock());
    say(message.as_bytes(),width,&mut writer).unwrap();
}
fn demo2(){//定义不可变变量
    let string = "TutorialsPoint";
    let flout = 4.5;
    let boolean = true;
    let char = "♥";

    println!("name is:{}",string);
    println!("rating  is:{}",flout);
    println!("growing  is:{}",boolean);
    println!("icon  is:{}",char);
}
fn demo3(){//可变变量
    let mut name = "哈哈";
    println!("名字是：{}",name);
    name="你好";
    println!("名字是：{}",name)
}
fn demo4(){//定义常量
    const name:&str ="你好";
    println!("名字是：{}",name);
    let cs = (10==20);
    println!("{}",cs);
}
fn demo5(){//if判断
    let  mut  num = 1;
    if num<5 {
        println!("小");
        num = num+1;
    }else if num>1 {
        println!("大")
    }else {
        println!("错误")
    }
}
fn demo6(){//match对应 语句 可以有返回值
    let code = "MH";
    let state = match  code{
        "MH"=>{println!("Found match for MH");"Maharashtra"},
        "KL"=>"Kerala",
        "KA"=>"Karnadaka",
        "GA"=>"Goa",
        _=>"Unknown"
    };
    println!("State name is {}",state)
}
fn demo7(){//for循环
    let mut x=0;

    /*for x in 1..10 {//从1到9
        println!("x是{}",x)
    }*/

    /*while x<11 {// x<11是一个表达式，返回的结果会转换为 布尔类型。只要 condition 返回真，那么循环就会一直执行。
        println!("x value is:{}",x);
        x+=1;
    }
    println!("x 外层 value is:{}",x);*/

    /*loop {//死循环
        x+=1;
        println!("x={}",x);
    }*/

    /*loop{//break 语句能够在 action 语句块中退出循环语句。
        x+=1;
        if x>10 {
            println!("x is:{}",x);
            break;
        }
        println!("x={}",x);
    }*/

    /*for x in 1..11 {//continue 语句能够退出当前循环，就是停止执行剩下的语句，直接进入下一个循环。
        if x==5 {
            continue;
        }
        println!("x is:{}",x);
    }*/
}

fn demo8(){//字符串使用
    let company:&str = "编程宝库";
    let location:&str = "中国";
    println!("公司名:{}----位于:{}",company,location);

    let   string = String::new();
    //let  mut string = String::new();
    //string.push_str("编程宝库");
    println!("长度是：{}",string.len());

    let   string2 = String::from("编程;宝库");
    println!("长度是:{}",string2.len());
    println!("{}",string2);
    println!("{}",string2.to_string());

    let mut  string3 =string2.replace("库","贝");// 查找并替换
    println!("{}",string3);
    string3.push_str("啊");
    println!("{}",string3);
    println!("长度是{}",string3.len());

    let string4:Vec<&str> = string3.split(";").collect();
    println!("分割---{},{}",string4[0],string4[1]);
}

fn demo9(){//数组的使用
    let arr=[10,11,12,88];
    println!("arr is :{:?}",arr);
    println!("arr size is :{:?}",arr.len());
    let arr2:[i32;4] = [-6;4];
    println!("arr2 is {:?}",arr2);
    println!("arr2 size is {:?}",arr2.len());

    for val in 0.. arr.len() {//for循环数组
        println!("val is {}",arr[val]);
    }
    for value in  arr2.iter() {//for循环数组
        println!("value is {}",value)
    }
    //可变数组
    let mut  arr3 =[45,78,16,18,72];
    arr3[0] = 77;
    println!("arr3 is {:?}",arr3);

    //值传递
    update(arr);

    println!("main {:?}",arr);

    //引用传递
    let mut  arr4 =[44,46,48];

    update2(&mut arr4);

    println!("main {:?}",arr4);

    const N: usize = 20;//20是长度
    // 固定大小
    let arr = [0; N];

    println!("N:{}",arr[19]);

    //元组
    let tuple:(i32,f64,u8) = (-15,4.99,87);
    println!("元组是：{:?}",tuple);
    println!("{}",tuple.0);
    println!("{}",tuple.1);
    println!("{}",tuple.2);

    let b:(i32,bool,f64) = (38,true,545.4);
    print(b);

}
fn print(x:(i32,bool,f64)){
    println!("print method");
    let (age,is_says,capa) = x;

    println!("age is {},stays is :{},capa is :{} ",age,is_says,capa);
}
fn update(mut arr:[i32;4]){
    for i in 0..arr.len() {
        arr[i]=0;
    }
    println!("update is {:?}",arr)
}

fn update2(arr:&mut [i32;3]){
    for x in 0..arr.len() {
        arr[x]=0;
    }
    println!("update is {:?}",arr)
}

fn demo10(){//切片
    let n1 = "setPathVal".to_string();
    println!("长度为:{},原值:{}",n1.len(),n1);

    let c1 = &n1[4..9];

    println!("{}",c1);

    let mut data =[10,20,30,40,50];

    use_slice(&mut data[1..4]);
    println!("{:?}",data)
}

fn use_slice(slice:&mut[i32]){
    println!("长度是：{}",slice.len());
    println!("{:?}",slice);
    slice[0]=11;
}


fn demo11(){//枚举
    let result = is_even(3);
    println!("第一次为3：{:?}",result);
    println!("第二次：{:?}",is_even(30));

    match is_even(5) {
        Some(data)=>{
            if data==true {
                println!("Even No")
            }
        },
        None =>{
            println!("not even")
        }
    }

    print_size(CarType::SUV);
    print_size(CarType::Sedan);
    print_size(CarType::Hatch);

}

enum CarType {
    Hatch,
    Sedan,
    SUV
}

fn print_size(car:CarType){
    match car {
        CarType::Hatch=>{
            println!("Small sized car")
        },
        CarType::Sedan=>{
            println!("medium sized car")
        },
        CarType::SUV=>{
            println!("Large sized Sports Utility car")
        }
    }
}

fn is_even(e:i32)->Option<bool>{
    if e %2 ==0 {
        Some(true)
    }else {
        None
    }
}

fn demo12(){//向量
    let  mut v = Vec::new();
    v.push(20);
    v.push(30);
    v.push(40);
    v.push(50);


    println!("长度是：{}",v.len());
    println!("{:?}",v);
    v.remove(3);
    println!("{:?}",v);

    if v.contains(&40) {
        println!("存在");
        for x in &v {
            println!("{}",x);
        }
        println!("{:?}",v)
    }
}

fn demo13(){//HashMap
    let mut stateCodes = HashMap::new();
    stateCodes.insert("name","小明");
    stateCodes.insert("age","19");
    stateCodes.insert("address","日不落");
    println!("{:?}",stateCodes);
    println!("{}", stateCodes.len());

    println!("年龄：{:?}",stateCodes.get("age"));
    println!("年龄：{:?}",stateCodes.get("ag"));//None

    match  stateCodes.get(&"nam"){//None
        Some(value)=>{
            println!("value is:{}",value);
        },
        None=>{
            println!("Value not found");
        }
    }

    for (key, val) in stateCodes.iter() {
        println!("key:{},val:{}",key,val);
    }

    if stateCodes.contains_key("name") {
        println!("value:{:?}",stateCodes.get("name"))
    }else {
        println!("valueNo:{:?}",stateCodes.get(""))
    }
    print!(" ADDRESS:{:?}",stateCodes.get("address"));
    stateCodes.remove(&"address");
    print!(" ADDRESS:{:?}",stateCodes.get("address"));
}

struct Employee {
    name:String,
    address:String,
    age:u32
}

struct Rectangle {
    width:u32,height:u32
}
struct Point {
    x: i32,
    y: i32,
}

fn demo14(){


    let mut   emp1 = Employee {
        name:String::from("李四"),
        address:String::from("南京"),
        age:26
    };
    emp1.age=30;

    let mut   emp2 = Employee {
        name:String::from("李"),
        address:String::from("南"),
        age:6
    };

    let small = Rectangle {
        width: 10,
        height: 20
    };



    // 为长方形结构体定义一个方法，用于计算当前长方形的面积
    impl Rectangle {

         fn area(&self)->u32{
             // 在方法内部，可以使用点号 `self.` 来访问当前结构体的元素
             self.width * self.height
         }
     }
    println!("width is {} height is {} area of Rectangle
   is {}",small.width,small.height,small.area());


    println!("Name:{},address:{},age:{}",emp1.name,emp1.address,emp1.age);
    println!("年龄:{}",emp1.age);

    let elder = who_is_elder(emp1,emp2);
    println!("elder is:");
    display(elder);

    let p1 = Point::getInstance(10,20);
    p1.display();

    impl Point{
        // 用于创建 Point 实例的静态方法
        fn getInstance(x:i32,y:i32) -> Point {
            Point{x:x,y:y}
        }
        // 用于显示结构体元素的普通方法
        fn display(&self){
            println!("x={} y={}",self.x,self.y);
        }
    }
}

fn display(emp:Employee){
    println!("Name is :{} address is {} age is
   {}",emp.name,emp.address,emp.age);
}

fn who_is_elder(emp1:Employee,emp2:Employee)->Employee{
    if emp1.age>emp2.age {
        return emp1;
    }else {
        return emp2;
    }
}















